package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl  extends ServiceImpl<StudentMapper, Student>implements StudentService {
     @Autowired
     public ClazzService clazzService;
    /**
     * 分页查询
     *
     * @param student
     * @return
     */
    @Override
    public PageResult<Student> findPage(StudentQueryParam student) {
        IPage<Student> pageFind=new Page<>(student.getPage(),student.getPageSize());
        //条件查询


        LambdaQueryWrapper<Student> queryWrapper= Wrappers.<Student>lambdaQuery()
                .like(student.getName()!=null,Student::getName,student.getName())
                .eq(student.getDegree()!=null,Student::getDegree,student.getDegree())
                .eq(student.getClazzId()!=null,Student::getClazzId,student.getClazzId())
                ;
        queryWrapper.orderByDesc(Student::getUpdateTime);
        IPage<Student> pageResult = this.page(pageFind,queryWrapper);
        long total = pageResult.getTotal();
        List<Student> studentList = pageResult.getRecords();//得到页面数据集合
        //封装
        //解决班级名称不显示问题
        List<Integer> clazzidList = studentList.stream().map(student1 -> student1.getClazzId()).toList();
        clazzidList=clazzidList.stream().distinct().toList();
        if(!CollectionUtils.isEmpty(clazzidList)){
            List<Clazz> clazzList = clazzService.listByIds(clazzidList);//所有班级字段集合
            studentList.forEach(student1 -> clazzList.stream().filter(clazz -> clazz.getId().equals(student1.getClazzId()))
                    .findFirst()
                    .ifPresent(clazz -> student1.setClazzName(clazz.getName()))
            );

        }
        return new PageResult<>(total,studentList);
    }

    /**
     * 新增学生
     *
     * @param student
     */
    @Override
    public void add(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        this.save(student);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Student getbyidStudent(Integer id) {
        Student byId = this.getById(id);
        return byId;
    }
}
